/**
 * Copyright (C) 2023 maminjie <canpool@163.com> (https://github.com/canpool)
 * Copyright (C) 2022-2023 Antonio Dias (https://github.com/antonypro)
 * SPDX-License-Identifier: MIT
 **/

#ifndef QGOODCENTRALWIDGET_H
#define QGOODCENTRALWIDGET_H

#include "qgoodbye_global.h"
#include "qgood/qgoodwindow.h"

#ifdef QGOOD_WINDOW_ENABLE
class QFrame;

class QGoodTitleBar;
#endif

/** **QGoodCentralWidget** class contains the public API's to control the behavior of **QGoodWindow**. */
class QGOODBYE_SHARED_EXPORT QGoodCentralWidget : public QWidget
{
    Q_OBJECT
public:
    /** Constructor of *QGoodCentralWidget*, is mandatory pass a valid instance of the parent *QGoodWindow*. */
    explicit QGoodCentralWidget(QGoodWindow *gw);
    /** Destructor of *QGoodCentralWidget*. */
    ~QGoodCentralWidget();

    /** Utility for showing a modal *QDialog* with customized title bar and borders.
        Pass the *QDialog* and the parent *QGoodWindow* and optionally a *QGoodCentralWidget* for mimic
        it's colors on the new window. Optionally pass a left title bar widget,
        a right title bar widget and set the visibility
        of title and icon on the new window. */
    static int execDialogWithWindow(QDialog *dialog, QGoodWindow *parentGw, QGoodCentralWidget *baseGcw = nullptr,
                                    QWidget *leftTitleBarWidget = nullptr, QWidget *rightTitleBarWidget = nullptr,
                                    bool titleVisible = true, bool iconVisible = true);

public Q_SLOTS:
    /** Set the title bar and the central widget unified. */
    void setUnifiedTitleBarAndCentralWidget(bool unified);

    /** Set the title bar mask, the title bar widgets masks united with this mask. */
    void setTitleBarMask(const QRegion &mask);

    /** Set the left title bar widget and returns the previous widget or nullptr if none, delete this widget as needed.
        If the widget is transparent for mouse, but not it's children's set \e transparentForMouse to true. */
    QWidget *setLeftTitleBarWidget(QWidget *widget, bool transparentForMouse = false);

    /** Set the right title bar widget and returns the previous widget or nullptr if none, delete this widget as needed.
        If the widget is transparent for mouse, but not it's children's set \e transparentForMouse to true. */
    QWidget *setRightTitleBarWidget(QWidget *widget, bool transparentForMouse = false);

    /** Set the center title bar widget and returns the previous widget or nullptr if none, delete this widget as
       needed. If the widget is transparent for mouse, but not it's children's set \e transparentForMouse to true. */
    QWidget *setCenterTitleBarWidget(QWidget *widget, bool transparentForMouse = false);

    /** Set the central widget of *QGoodCentralWidget*. */
    void setCentralWidget(QWidget *widget);

    /** Set the alignment of *QGoodCentralWidget* title on the title bar.
        Note: If align to center and also set a central title bar widget
        the title will be aligned to the left.*/
    void setTitleAlignment(const Qt::Alignment &alignment);

    /** Set the color of *QGoodCentralWidget* title bar. */
    void setTitleBarColor(const QColor &color);

    /** Set the color of *QGoodCentralWidget* border. */
    void setActiveBorderColor(const QColor &color);

    /** Change the visibility of *QGoodCentralWidget* title bar. */
    void setTitleBarVisible(bool visible);

    /** Change the visibility of *QGoodCentralWidget* caption buttons. */
    void setCaptionButtonsVisible(bool visible);

    /** Change the visibility of *QGoodCentralWidget* title bar title. */
    void setTitleVisible(bool visible);

    /** Change the visibility of *QGoodCentralWidget* title bar icon. */
    void setIconVisible(bool visible);

    /** Change the width of *QGoodCentralWidget* title bar icon,
     * width higher than 0 will hide the *QGoodCentralWidget* provided icon,
     * this is useful when showing own icons. */
    void setIconWidth(int width);

    /** Change the title bar height to \e height multiplied to current pixel ratio. */
    void setTitleBarHeight(int height);

    /** Change the caption button width to \e width multiplied to current pixel ratio. */
    void setCaptionButtonWidth(int width);

    /** Returns if the title bar and the central widget are unified. */
    bool isUnifiedTitleBarAndCentralWidget() const;

    /** Returns the left *QGoodCentralWidget* title bar widget or nullptr if none is set. */
    QWidget *leftTitleBarWidget() const;

    /** Returns the right *QGoodCentralWidget* title bar widget or nullptr if none is set. */
    QWidget *rightTitleBarWidget() const;

    /** Returns the center *QGoodCentralWidget* title bar widget or nullptr if none is set. */
    QWidget *centerTitleBarWidget() const;

    /** Returns the *QGoodCentralWidget* central widget or nullptr if none is set. */
    QWidget *centralWidget() const;

    /** Returns the alignment of *QGoodCentralWidget* title on the title bar. */
    Qt::Alignment titleAlignment() const;

    /** Returns the *QGoodCentralWidget* title bar color. */
    QColor titleBarColor() const;

    /** Returns the *QGoodCentralWidget* border color. */
    QColor activeBorderColor() const;

    /** Returns if the *QGoodCentralWidget* title bar is visible or not. */
    bool isTitleBarVisible() const;

    /** Returns if the *QGoodCentralWidget* caption buttons are visible or not. */
    bool isCaptionButtonsVisible() const;

    /** Returns if the *QGoodCentralWidget* title bar title is visible or not. */
    bool isTitleVisible() const;

    /** Returns if the *QGoodCentralWidget* title bar icon is visible or not. */
    bool isIconVisible() const;

    /** Returns the customized icon width. */
    int iconWidth() const;

    /** Returns the *QGoodCentralWidget* title bar height. */
    int titleBarHeight() const;

    /** Returns the *QGoodCentralWidget* title bar caption button width. */
    int captionButtonWidth() const;

    /** Update the *QGoodCentralWidget* state, it's called internally
        and must be called if changing *QGoodCentralWidget* metrics
        like hide or show a title bar widget. */
    void updateWindow();
protected:
    bool eventFilter(QObject *watched, QEvent *event);
private:
    QPointer<QGoodWindow> m_gw;
    QPointer<QWidget> m_centralWidgetPlaceHolder;
    QPointer<QWidget> m_centralWidget;
#ifdef QGOOD_WINDOW_ENABLE
    bool m_unifiedTitleBarAndCentralWidget;
    QRegion m_titleBarMask;
    bool m_leftWidgetTransparentForMouse;
    bool m_rightWidgetTransparentForMouse;
    bool m_centerWidgetTransparentForMouse;
    int m_captionButtonWidth;
    bool m_drawBorders;
    QFrame *m_frame;
    QString m_frameStyle;
    QGoodTitleBar *m_titleBar;
    QPointer<QWidget> m_titleBarLeftWidget;
    QPointer<QWidget> m_titleBarRightWidget;
    QPointer<QWidget> m_titleBarCenterWidget;
    QColor m_titleBarColor;
    QColor m_activeBorderColor;
    bool m_titleBarVisible;
    bool m_captionButtonsVisible;
    bool m_titleVisible;
    bool m_iconVisible;
    int m_iconWidth;
#endif
};

#endif   // QGOODCENTRALWIDGET_H
